SQL Server: Optimizing Stored Procedure Performance

If you want to use SQL Server databases effectively, you will end up using stored procedures. Their aim is that you have optimized and compiled code that resides in a cache to improve workload performance for subsequent executions. However, you might find that it does not always work out that way. The performance of a stored procedure is heavily dependent on how the plan is chosen and cached. If the plan is not optimal for subsequent executions it could cause performance to suffer greatly. In the end, you might suffer from what is known as parameter-sniffing-problems where the optimal plan for a procedure varies based on the parameters supplied. This comprehensive course will teach you how stored procedure plan caching works and how to get SQL Server to produce the optimal plan for your stored procedure performance in a variety of circumstances. The course is applicable to SQL Server developers and anyone who is responsible for writing stored procedures that must repeatedly perform well. The demo database provided is compatible with SQL Server 2008 through SQL Server 2014. All course demos are shown on SQL Server 2014 with references to behaviors (where different) on all versions. Optimization strategies you will learn in the course apply to SQL Server 2005 onward, and some even back to SQL Server 2000.
SQL Server: Optimizing Stored Procedure PerformanceIntroduction00:21:19
Introduction01:51
This Course02:35
What Does Optimizing Stored Procedure Performance Mean?02:49
Does This Sound Familiar?02:11
What Does Optimizing Stored Procedure Performance NOT Mean?03:46
Why This Course Is Relevant03:11
Course Focus and Structure (1)02:09
Course Focus and Structure (2)02:44
SQL Server: Optimizing Stored Procedure PerformanceWhy Use Stored Procedures?00:27:04
Overview02:04
Different Ways to Execute SQL Statements02:25
Some Statements Can Be Cached for Reuse (1)01:45
Some Statements Can Be Cached for Reuse (2)03:43
Version-Specific Plan Caching Whitepapers01:26
Reducing Plan Cache Pollution04:32
Understanding sp_executesql02:51
Stored Procedures / sp_executesql and the Cache02:39
Parameter Sniffing02:01
Summary: Why Use Stored Procedures?03:34
SQL Server: Optimizing Stored Procedure PerformanceCreation, Compilation, and Invalidation Section 101:08:12
Overview02:15
What Happens When You Create a Procedure?03:22
Where Are Stored Procedures Stored? (1)01:53
Where Are Stored Procedures Stored? (2)01:38
Creating Stored Procedures02:47
Stored Procedure Plans and Caching05:00
Side Effect: Plan Cache Flush (1)02:35
Side Effect: Plan Cache Flush (2)01:20
Side Effect: Plan Cache Flush (3)03:09
Side Effect: Plan Cache Flush (4)02:37
DEMO: Generic Demo Intro01:43
DEMO: Setup Credit Sample Database05:20
DEMO: Setup Analysis Procedures for Credit07:35
DEMO: Object-Level Invalidation or Eviction16:11
DEMO: Database-Level Invalidation or Eviction03:43
DEMO: Server-Level Invalidation or Eviction06:55
SQL Server: Optimizing Stored Procedure PerformanceCreation, Compilation, and Invalidation Section 200:53:29
Plan Invalidation05:09
Plan Invalidation Due to Statistics Updates04:16
DEMO: Plan Invalidation and Statistics - Part 114:23
Updates to Statistics may not Invalidate Bad Plans03:01
DEMO: Plan Invalidation and Statistics - Part 213:30
Plan Invalidation / Recompilation Causes02:31
Stored Procedure Caching + Compilation Concerns04:06
When Should You Recompile?03:45
Summary: Creation and Compilation02:45
SQL Server: Optimizing Stored Procedure PerformanceOptimization and Recompilation Section 100:55:44
Overview04:24
Options for Recompilation04:11
sp_recompile object_name06:52
DEMO: Long Blocking Chains11:13
CREATE ... WITH RECOMPILE03:21
DEMO: CREATE ... WITH RECOMPILE05:22
Conditional Logic02:49
DEMO: Conditional Logic - Part 108:43
Modularization03:17
DEMO: Conditional Logic - Part 2 - Modularization05:27
SQL Server: Optimizing Stored Procedure PerformanceOptimization and Recompilation Section 200:51:43
EXECUTE ... WITH RECOMPILE (1)03:38
DEMO: EXECUTE ... WITH RECOMPILE For Testing10:18
EXECUTE ... WITH RECOMPILE (2)03:39
DEMO: Determining Plan Stability07:44
Statement-Level Recompilation05:33
DEMO: Recompilation With OPTION (RECOMPILE)04:15
DEMO: Recompilation With OPTION (Optimize For Literal)04:23
DEMO: Recompilation With OPTION (Optimize For UNKNOWN)09:17
DEMO: Variables Simulate Optimize For UNKNOWN02:51
SQL Server: Optimizing Stored Procedure PerformanceOptimization and Recompilation Section 301:08:24
OPTION (RECOMPILE)03:56
OPTIMIZE FOR ...04:41
DEMO: Scenario - Setup04:15
DEMO: Scenario - Scenario Explained05:26
DEMO: Scenario - Conditional Logic Does NOT Work05:46
DEMO: Scenario - EXEC With RECOMPILE07:20
DEMO: Scenario - Conditional Logic Plus Modularization WORKS!06:32
DEMO: Stabilizing Plans With Indexes - Setup and Scenario04:36
DEMO: Stabilizing Plans With Indexes - Plan Stability10:23
DEMO: Stabilizing Plans With Indexes - Voila!10:17
Summary: Walkthrough Demo (1)02:20
Summary: Walkthrough Demo (2)02:46
SQL Server: Optimizing Stored Procedure PerformanceOptimization and Recompilation Section 401:02:20
When Does a Procedure Get Optimized?03:39
Server-Wide: OPTIMIZE FOR UNKNOWN03:08
The Checkered Past of OPTION (RECOMPILE)03:37
Dynamic String Execution03:09
DEMO: Dynamic String Execution08:04
Multi-Purpose Procedures02:41
Building Strings Dynamically and Caching03:44
DEMO: Multi-Purpose Procedures - Setup and Scenario05:21
DEMO: Multi-Purpose Procedures - The Problem05:08
DEMO: Multi-Purpose Procedures - Option Recompile too Much CPU03:15
DEMO: Multi-Purpose Procedures - The Ultimate Multi-Purpose Proc16:47
Patterns and Practices in Statement Recompilation02:59
Summary: Stored Procedure Pitfalls/Performance00:43
SQL Server: Optimizing Stored Procedure PerformanceOther Concerns and Considerations00:23:17
Overview03:46
Demystifying Plan Caching for Bad Plans03:04
Secondary Concerns and Considerations07:57
Optimizing Statement and Procedure Performance02:35
Just the Tip of the Iceberg04:19
Course Summary01:33